package com.green.voluntaryfiling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.voluntaryfiling.common.DataGridView;
import com.green.voluntaryfiling.domain.VolunteerFilled;
import com.green.voluntaryfiling.mapper.VolunteerFilledMapper;
import com.green.voluntaryfiling.service.VolunteerFilledService;
import com.green.voluntaryfiling.vo.Major;
import com.green.voluntaryfiling.vo.VoluntaryFillingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2022/04/04/15:50
 * @Description: 
 */


@Service
public class VolunteerFilledServiceImpl extends ServiceImpl<VolunteerFilledMapper, VolunteerFilled> implements VolunteerFilledService{

    @Autowired
    private VolunteerFilledMapper volunteerFilledMapper;

    /**
     * 存储记录
     * @param voluntaryFillingVo
     * @return
     */
    @CachePut(cacheNames = "com.green.voluntaryfiling.service.impl.VolunteerFilledServiceImpl",key = "#result.id")
    @Override
    public VolunteerFilled saveRecord(VoluntaryFillingVo voluntaryFillingVo) {
        VolunteerFilled volunteerFilled = new VolunteerFilled();
        //补充
        volunteerFilled.setUsername(voluntaryFillingVo.getUsername());
        volunteerFilled.setIsRansfers(voluntaryFillingVo.getIs_ransfers());
        volunteerFilled.setUniversityOrderId(voluntaryFillingVo.getUniversity_order_id());
        volunteerFilled.setUniversityName(voluntaryFillingVo.getUniversity_name());
        int i=0;
        for (Major major : voluntaryFillingVo.getMajorList()) {
            i++;
            if (major==null){
                break;
            }
            switch (i){
                case 1:
                    volunteerFilled.setFirstMajorCode(major.getOrder());
                    volunteerFilled.setFirstMajorName(major.getMajor());
                    break;
                case 2:
                    volunteerFilled.setSecondMajorCode(major.getOrder());
                    volunteerFilled.setSecondMajorName(major.getMajor());
                    break;
                case 3:
                    volunteerFilled.setThirdMajorCode(major.getOrder());
                    volunteerFilled.setThirdMajorName(major.getMajor());
                    break;
                case 4:
                    volunteerFilled.setForthMajorCode(major.getOrder());
                    volunteerFilled.setForthMajorName(major.getMajor());
                    break;
                case 5:
                    volunteerFilled.setFifthMajorCode(major.getOrder());
                    volunteerFilled.setFifthMajorName(major.getMajor());
                    break;
                case 6:
                    volunteerFilled.setSixthMajorCode(major.getOrder());
                    volunteerFilled.setSixthMajorName(major.getMajor());
                    break;
                default:
                    break;
            }
        }
        //保存
        this.volunteerFilledMapper.insert(volunteerFilled);
        return volunteerFilled;
    }



    /**
     * 更新记录
     * @param voluntaryFillingVo
     * @return
     */
    @CachePut(cacheNames = "com.green.voluntaryfiling.service.impl.VvolunteerFilledServiceImpl",key = "#result.id")
    @Override
    public VolunteerFilled updateRecord(VoluntaryFillingVo voluntaryFillingVo) {
        //已有ID号无需查找或无ID需要查找
        VolunteerFilled volunteerFilled;
        if (voluntaryFillingVo.getId()!=null){
            volunteerFilled = new VolunteerFilled();
            volunteerFilled.setId(voluntaryFillingVo.getId());
        }else {
            QueryWrapper<VolunteerFilled> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(voluntaryFillingVo.getUsername()), "username",voluntaryFillingVo.getUsername());
            queryWrapper.eq(StringUtils.isNotBlank(voluntaryFillingVo.getUniversity_name()), "university_name",voluntaryFillingVo.getUniversity_name());
            volunteerFilled = this.volunteerFilledMapper.selectOne(queryWrapper);
        }

        //补充
        volunteerFilled.setUsername(voluntaryFillingVo.getUsername());
        volunteerFilled.setIsRansfers(voluntaryFillingVo.getIs_ransfers());
        volunteerFilled.setUniversityOrderId(voluntaryFillingVo.getUniversity_order_id());
        volunteerFilled.setUniversityName(voluntaryFillingVo.getUniversity_name());

        volunteerFilled.setFirstMajorCode(null);
        volunteerFilled.setFirstMajorName(null);
        volunteerFilled.setSecondMajorCode(null);
        volunteerFilled.setSecondMajorName(null);
        volunteerFilled.setThirdMajorCode(null);
        volunteerFilled.setThirdMajorName(null);
        volunteerFilled.setForthMajorCode(null);
        volunteerFilled.setForthMajorName(null);
        volunteerFilled.setFifthMajorCode(null);
        volunteerFilled.setFifthMajorName(null);
        volunteerFilled.setSixthMajorCode(null);
        volunteerFilled.setSixthMajorName(null);

        int i=0;
        for (Major major : voluntaryFillingVo.getMajorList()) {
            if (major==null){
                break;
            }
            i++;
            switch (i){
                case 1:
                    volunteerFilled.setFirstMajorCode(major.getOrder());
                    volunteerFilled.setFirstMajorName(major.getMajor());
                    break;
                case 2:
                    volunteerFilled.setSecondMajorCode(major.getOrder());
                    volunteerFilled.setSecondMajorName(major.getMajor());
                    break;
                case 3:
                    volunteerFilled.setThirdMajorCode(major.getOrder());
                    volunteerFilled.setThirdMajorName(major.getMajor());
                    break;
                case 4:
                    volunteerFilled.setForthMajorCode(major.getOrder());
                    volunteerFilled.setForthMajorName(major.getMajor());
                    break;
                case 5:
                    volunteerFilled.setFifthMajorCode(major.getOrder());
                    volunteerFilled.setFifthMajorName(major.getMajor());
                    break;
                case 6:
                    volunteerFilled.setSixthMajorCode(major.getOrder());
                    volunteerFilled.setSixthMajorName(major.getMajor());
                    break;
                default:
                    break;
            }
        }
        //更新
//      this.volunteerFilledMapper.updateById(volunteerFilled);
        volunteerFilledMapper.updateRecord(volunteerFilled);
        return volunteerFilled;
    }

    /**
     * 查询全部信息(个人)
     * @param username
     * @return
     */
    @Override
    public DataGridView queryALLInfoFormUser(String username) {
        IPage<VolunteerFilled> page = new Page<>();
        QueryWrapper<VolunteerFilled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(username),"username",username);
        this.volunteerFilledMapper.selectPage(page,queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
